package com.yangjie.autotest.component;

import java.io.File;

import javax.inject.Inject;
import javax.inject.Named;

import org.springframework.context.annotation.Scope;

import com.yangjie.autotest.testcase.xml.Param;
import com.yangjie.autotest.testcase.xml.Params;
import com.yangjie.log.LoggerIF;

@Named("fileComp")
@Scope("prototype")
public class FileComponent {
	@Inject
	private LoggerIF logger = null;
	
	
	String basePath = null;
	
	public void init(String basePath) {
		this.basePath = basePath;
	}
	
	public void deleteFile(Params params) throws Exception{
		for(Param param: params.getParam()) {
			deleteFile(param.getValue());
		}
	}
	
	private void deleteFile(String fileName) throws Exception{
		try {
			String filePath = basePath + File.separator + fileName;
			File file = new File(filePath);
			if(!file.exists()) {
				logger.debug("delete file " + filePath + " not exists" );
				return;
			}
			if (file.isDirectory()) {
				logger.debug(filePath + " is directory, will not be deleted");
				return;
			}
			logger.debug("delete file " + filePath );
			file.delete();
		} catch(Exception e) {
			e.printStackTrace();
			throw e;
		}
	}
}
